package actions;

import java.awt.event.ActionEvent;

import client.ClientSocketConnection;
import client_gui.ClientWindow;
import client_tree.User;

public class UnblockUserAction extends RLMAbstractAction {

	public UnblockUserAction(ClientWindow owner) {
		setActionOwner(owner);
//		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0));
		putValue(SMALL_ICON, loadIcon("images/unblock.png"));
		putValue(NAME, "Unblock User");
		putValue(SHORT_DESCRIPTION, "Deblokiranje korisnika");
	}
	
	
	public void actionPerformed(ActionEvent e) {

		ClientSocketConnection connection = getActionOwner().getApplicaton().retrieveSocketConnection();

		User user = getActionOwner().getSelectedUser();
		
		StringBuffer unblockCommand = new StringBuffer();
		
		unblockCommand.append("UNBLOCK ");
		unblockCommand.append(getActionOwner().getApplicaton().getClientUser().getUsername());
		unblockCommand.append(" ");
		unblockCommand.append(user.getUsername());
		unblockCommand.append(" ");
		unblockCommand.append(getActionOwner().getApplicaton().getClientUser().getNickname());
		unblockCommand.append(" ");
		unblockCommand.append(getActionOwner().getApplicaton().getClientUser().getEmail());
		unblockCommand.append(" ");
		unblockCommand.append(STATUS_NAMES[getActionOwner().getApplicaton().getClientUser().getStatus()]);
		unblockCommand.append(" ");
		unblockCommand.append(getActionOwner().getApplicaton().getClientUser().getAvatar());
		
		String unblockCommandCompleted = unblockCommand.toString();

		connection.setMessage(UNBLOCK_CMD, unblockCommandCompleted);
		
		String response = connection.buffers[UNBLOCK_CMD].read();
		
		if (response.startsWith("100")) {
			getActionOwner().getApplicaton().getClientUser().removeFromBlockList(user.getUsername());
			getActionOwner().refreshTree();
		}
		
	}
	
}
